//package com.hshx.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
////import com.alibaba.nacos.common.utils.StringUtils;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//
//@Component
//public class MyFilter implements GlobalFilter, Ordered {
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        String token = exchange.getRequest().getQueryParams().getFirst("token");
//        String header = exchange.getRequest().getHeaders().getFirst("token1");
//        System.out.println("MyFilter:"+token+":"+header);
//        //返回json
////        if(StringUtils.isEmpty(header)){
////            ServerHttpResponse response = exchange.getResponse();
////            //response.setStatusCode(HttpStatus.UNAUTHORIZED);
////            //return exchange.getResponse().setComplete();
////            JSONObject message = new JSONObject();
////            message.put("status", -1);
////            message.put("data", "鉴权失败");
////            byte[] bits = message.toJSONString().getBytes(StandardCharsets.UTF_8);
////            DataBuffer buffer = response.bufferFactory().wrap(bits);
////            response.setStatusCode(HttpStatus.OK);
////            //指定编码，否则在浏览器中会中文乱码
////            response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
////            System.out.println("MyFilter工作中");
////
////            return response.writeWith(Mono.just(buffer));
////            //return response.writeWith(Flux.just(buffer));
////        }
////        ServerHttpRequest request = exchange.getRequest();
////        ServerHttpResponse response = exchange.getResponse();
////        //1、获取当前的请求连接
////        String path = request.getURI().getPath();
////        System.out.println("path:"+path);
////        //2、判断，此请求地址是否需要进行token校验，如果不需要，直接放行，进入微服务
////        if(path.contains("coursePub/preview") ||
////                path.contains("login") ||
////                path.contains("basic/dictionary") ||
////                path.contains("category/tree-nodes")||
////                path.contains("course/upload")||
////                path.contains("search/")){
////            return chain.filter(exchange);
////        }
//        //3、获取请求头参数token
////        String token = request.getHeaders().getFirst("Authorization");
////        Boolean verifyToken = JwtUtils.verifyToken(token);
//        //4、验证token是否合法，如果不合法，响应状态码401
////        if(!verifyToken) {
////            response.setStatusCode(HttpStatus.UNAUTHORIZED);
////            return response.setComplete(); //直接返回
////        }
//        //5、解析token，获取登录时存入的数据（userId，companyId，companyName）
////        Claims claims = JwtUtils.parserToken(token).getBody();
////        Long userId = claims.get("userId", Long.class);
////        Long companyId = claims.get("companyId", Long.class);
////        String companyName = claims.get("companyName", String.class);
////        //转化java对象
////        AuthInfo info = new AuthInfo();
////        info.setUid(userId);
////        info.setCompanyName(companyName);
////        info.setCompanyId(companyId);
////        //6、设置一个新的请求头参数（解析后的明文数据）
////        String json = URLEncoder.encode(JSON.toJSONString(info),"UTF-8");
//        //创建一个httpRequest的请求对象
////        ServerHttpRequest httpRequest = request.mutate().headers(httpHeaders -> {
////            httpHeaders.set("payload", json);
////        }).build();
//        //将此请求对象，写入到微服务转发的上下文中
////        exchange.mutate().request(httpRequest);
//        System.out.println("MyFilter执行.......");
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 1;
//    }
//}
